<?php
Prado::using('Application.common.common');
class EditBanner extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		if (!$this->IsPostBack)
		{
			$this->Master->pageHead->Title = "Admin | Edit Banner";
			$this->AdminMenu->lnkManageBanner->CssClass = "active";
			
			if ($this->Request->contains('m')) {
				$this->Notice->Type = UserNoticeType::NoticeAdmin;
				if ($this->Request['m'] == 'cb')
					$this->Notice->Text = $this->Application->getModule('message')->translate('ADD_SUCCESS','Banner','');
				else if ($this->Request['m'] == 'ub')	
					$this->Notice->Text = $this->Application->getModule('message')->translate('UPDATE_SUCCESS','Banner','');
				
			}
			
			$this->txtBannerID->Value = ($this->Request->contains('bid')) ? TPropertyValue::ensureInteger($this->Request['bid']) : 0;	
			$bannerRecord = BannerRecord::finder()->findByPk($this->txtBannerID->Value);
			if ($bannerRecord instanceof BannerRecord)
			{
				$this->txtBannerName->Text=$bannerRecord->BannerName;
				$this->txtBannerLink->Text=$bannerRecord->BannerURL;
				$this->imgBannerPhoto->ImageUrl = $this->Request->UrlManagerModule->UrlPrefix."/useruploads/photos/banner/slideshow/".$bannerRecord->BannerPhoto;
			}
		}
	}
	
	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{						
			try {					
				$user = UserRecord::finder()->findByPk($this->Application->User->ID);
				
				$bannerRecord = BannerRecord::finder()->findByPk($this->txtBannerID->Value);
				$bannerRecord->BannerName = $this->txtBannerName->SafeText;
				$bannerRecord->BannerURL = $this->txtBannerLink->SafeText;	
				$bannerRecord->BannerType = "1"; // homepage slideshow banner
				$bannerRecord->Status = "1";
				
				$hashImage = "";
				$currentYear = date("Y");
				$currentMonth = date("m");
				$filePath = dirname($this->Request->ApplicationFilePath).DIRECTORY_SEPARATOR."useruploads".DIRECTORY_SEPARATOR."photos".DIRECTORY_SEPARATOR."banner".DIRECTORY_SEPARATOR."slideshow".DIRECTORY_SEPARATOR;
				
				if($this->fuBannerPhoto->HasFile) 
				{			
					$hashStr=md5(uniqid(time()));
					$imageExt=strtolower(array_pop(explode('.',$this->fuBannerPhoto->FileName)));
					$hashImage=$hashStr.'.'.$imageExt;		
					$this->fuBannerPhoto->saveAs($filePath.$hashImage, true);

					$bannerRecord->BannerPhoto=$hashImage;
				}
					
				$bannerRecord->CreateBy = $bannerRecord->ModifyBy = $this->Application->User->ID;
				$bannerRecord->CreateDate = $bannerRecord->ModifyDate = time();									
				$bannerRecord->save();						
				$this->Response->redirect($this->Service->ConstructUrl('admin.EditBanner', array('bid'=>$bannerRecord->ID, 'm'=>'ub')));			
			}
			catch(TException $e)
			{
				$this->Notice->Type = UserNoticeType::Error;
				$this->Notice->Text = $e;
			}
		}
	}
	
	protected function btnCancel_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("admin.ManageBanners"));				
	}
}

?>